<?php
namespace app\service;
use think\facade\Db;
class deviceService{
    public  function userdeviceList($user_id){
        $userinfo=Db::table("personnel")->where("user_id",$user_id)->find();
        if($userinfo["relation"]=="5"){
            $locklist=Db::table("device")->where("community_id",$userinfo["community_id"])->select();
            return $locklist;
        }else{
            //小区大门
            $locklist1=Db::table("device")->where("community_id",$userinfo["community_id"])->where("devicetype",1)->select();

            $locklist2= Db::table("device")
                ->where("devicetype",2)
                ->where("community_id",$userinfo["community_id"])
                ->where("group",$userinfo["group"])
                ->where("building",$userinfo["building"])
                ->where("unit",$userinfo["unit"])
                ->select();
            $locklist3= Db::table("device")
                ->where("devicetype",3)
                ->where("community_id",$userinfo["community_id"])
                ->where("group",$userinfo["group"])
                ->where("building",$userinfo["building"])
                ->where("unit",$userinfo["unit"])
                ->select();
            $list=array_merge($locklist1,$locklist2,$locklist3);
            return $list;
        }

    }
    public  function visitordeviceList($user_id){
        $userinfo=Db::table("visitor")->where("user_id",$user_id)->where("state","1")->find();
        //小区大门
        $locklist1=Db::table("device")->where("community_id",$userinfo["community_id"])->where("devicetype",1)->select();
        
        $locklist2= Db::table("device")
                        ->where("devicetype",2)
                        ->where("community_id",$userinfo["community_id"])
                        ->where("group",$userinfo["group"])
                        ->where("building",$userinfo["building"])
                        ->where("unit",$userinfo["unit"])
                        ->select();
        $locklist3= Db::table("device")
            ->where("devicetype",2)
            ->where("community_id",$userinfo["community_id"])
            ->where("group",$userinfo["group"])
            ->where("building",$userinfo["building"])
            ->where("unit",$userinfo["unit"])
            ->select();
        $list=array_merge($locklist1,$locklist2,$locklist3);
        return $list;
    }
    public  function userdeviceListPid($id){
        $userinfo=Db::table("personnel")->where("id",$id)->find();
        //小区大门
        $locklist1= $locklist=Db::table("device")->where("devicetype",1)->select();
        
        $locklist2= Db::table("device")
                        ->where("devicetype",2)
                        ->where("building",$userinfo["building"])
                        ->where("unit",$userinfo["unit"])
                        ->select();
        $locklist3= Db::table("device")
            ->where("devicetype",3)
            ->where("building",$userinfo["building"])
            ->where("unit",$userinfo["unit"])
            ->select();
        $list=array_merge($locklist1,$locklist2,$locklist3);
        return $list;
    }
	public function deviceList($com){
        return Db::name('device')->where($com)->order('id desc')->paginate(15);
    }
    public function devicecount($whe){
    	return Db::name('device')->where($whe)->count();
    }
    public function communityList($whe){
        return Db::name('community')->order('id desc')->where($whe)->select();
    }
    public function communityName($comid){
        return Db::name('community')->where('id',$comid)->find();
    }

    public function deviceinfo($devicekey){
        return Db::name('device')->where('devicekey',$devicekey)->find();
    }
    public function deviceAdd($data){
        return Db::name('device')->insert($data);
    }

    //根据房屋id查询可以授权的设备
    public function deviceaccredit($room_id){
        $roominfo=Db::table("room")->where("id",$room_id)->find();
        $locklist1= $locklist=Db::table("device")->where("devicetype",1)->select();
        
        $locklist2= Db::table("device")
                        ->where("devicetype",2)
                        ->where("community_id",$roominfo["community_id"])
                        ->where("group",$roominfo["group"])
                        ->where("building",$roominfo["building"])
                        ->where("unit",$roominfo["unit"])
                        ->select();
        $locklist3= Db::table("device")
            ->where("devicetype",3)
            ->where("community_id",$roominfo["community_id"])
            ->where("group",$roominfo["group"])
            ->where("building",$roominfo["building"])
            ->where("unit",$roominfo["unit"])
            ->select();
        $list=array_merge($locklist1,$locklist3);
        return $list;
        
    }
}